<?php
/**
 * @file $HeadURL: jobcategory.php $
 * @author $Author: ZhangFan $
 * @date $Date: 2015-12-14 $
 * @brief
 */

namespace  haibao\cms\business;

use \haibao\cms\data\JobCategory as JobCateData;
use \haibao\cms\model\filter\JobCategory as JobCateFilter;
use \haibao\frame\data\query\Condition;
class JobCategory extends \haibao\cms\business\Base{
	
	public static function addJobCategory($model){
		self::checkOperation(self::FUNC_CODE_JOB_CATEGORY_ADD);
		if(empty($model->CateName)){
			throw new BusinessException('请添加岗位类型名称');
		}
		$jobCateData = new JobCateData();
		$jobCateData->add($model);
		
		self::saveOperateHistory(self::FUNC_CODE_JOB_CATEGORY_ADD, $model->toArray(false));
	}
	
	public static function getModelById($jobCateId){
	    if(!$jobCateId){
	        throw new BusinessException('岗位类型Id不能为空');
	    }
	    $jobCateData = new JobCateData();
	    return $jobCateData->getOneById($jobCateId);
	}
	public static function getJobCategoryList($jobCateFilter){
	    $jobCateData = new JobCateData();
	    return $jobCateData->getAll($jobCateFilter);
	}
	public static function updateJobCategory($model){
	    self::checkOperation(self::FUNC_CODE_JOB_CATEGORY_EDIT);
	    if(empty($model->CateName)){
	        throw new BusinessException('请添加岗位类型名称');
	    }
	    $jobCateData = new JobCateData();
	    $jobCateData->updateById($model->Id, $model);
	    self::saveOperateHistory(self::FUNC_CODE_JOB_CATEGORY_EDIT, $model->toArray(false));
	}
	public static function changeJobCategoryStatus($id, $status){
	    self::checkOperation(self::FUNC_CODE_JOB_CATEGORY_DEL);
	    if(!$id){
	        throw new BusinessException('岗位类型Id不能为空');
	    }
	    $jobCateData = new JobCateData();
	    $jobCateData->changeJobCateStatusById($id, $status);
	    self::saveOperateHistory(self::FUNC_CODE_JOB_CATEGORY_DEL, array($id,$status));
	}
	public static function hasSameCateName($cateName,$cateId= NULL){
	    $hased = false;
	    $jobCateFilter = new JobCateFilter();
	    if(!$cateName){
	        throw new BusinessException('岗位类型名称不能为空');
	    }
	    if($cateName){
	        $jobCateFilter->where(JobCateFilter::CONDITION_FIELD_NAME_CATENAME, Condition::CONDITION_EQUAL, $cateName);
	    }
	    $jobCateFilter->select(array(
	        JobCateFilter::CONDITION_FIELD_NAME_CATENAME,
	        JobCateFilter::CONDITION_FIELD_NAME_ID
	    ));
	    $jobCateData = new JobCateData();
	    $jobCateInfo = $jobCateData->getOne($jobCateFilter);
	    if($jobCateInfo && $jobCateInfo->Id != $cateId){
	        $hased = true;
	    }
	    return $hased;
	}
}